/*
 * Copyright © 2004-2014 chenYuan. All rights reserved.
 * @Website:wwww.jspx.net
 * @Mail:39793751@qq.com
 * @author: chenYuan , 陈原
 * @License: Jspx.net Framework Code is open source (LGPL)，Jspx.net Framework 使用LGPL 开源授权协议发布。
 * @jvm:jdk1.6+  x86/amd64
 *
 */
package com.jspx.txweb.result;

import com.jspx.txweb.ActionInvocation;
import com.jspx.txweb.dispatcher.Dispatcher;
import com.jspx.txweb.support.ActionSupport;
import com.jspx.txweb.util.TXWebUtil;
import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.io.xml.DomDriver;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletResponse;
import java.io.*;

/**
 * Created by IntelliJ IDEA.
 * User:chenYuan (mail:39793751@qq.com)
 * Date: 2007-3-20
 * Time: 17:25:23
 * bean转换为xml输出，转换规则看 XStream
 */
public class XStreamResult extends ResultSupport
{
    public XStreamResult()
    {

    }

    public void execute(ActionInvocation actionInvocation) throws IOException, ServletException
    {
        ActionSupport action = actionInvocation.getActionProxy().getAction();
        HttpServletResponse response = action.getResponse();
        checkCache(action,response);
        Object obj = action.getResult();
        if (obj==null)
        {
            TXWebUtil.print("<?xml version=\"1.0\" encoding=\"" + Dispatcher.getEncode() + "\"?>\r\n<null/>", TXWebUtil.xmlType, response);
            return;
        }
        XStream xstream = new XStream(new DomDriver());
        xstream.alias(obj.getClass().getSimpleName(),obj.getClass());//为类名节点重命名
        xstream.toXML(obj,response.getOutputStream());
   }
}